home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / alpha_blending / blend_equations.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.3 KB  |  343 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*     blend_equations.c 
  19.  *
  20.  *    Demonstrates the use of the blend_minmax, 
  21.  *    blend_subtract, and blend_logic_op extensions using 
  22.  *    glBlendEquationEXT.
  23.  *
  24.  *    Over a two-color backround, draw rectangles using twelve new 
  25.  *    blend options.  The values are read back as UNSIGNED_BYTE 
  26.  *    and printed in hex over each value.  These values are useful 
  27.  *    for logic op comparisons when channels are 8 bits deep.
  28.  *
  29.  *    Escape key            - exit program
  30.  */
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35.  
  36. #include <GL/gl.h>
  37. #include <GL/glu.h>
  38. #include <GL/glut.h>
  39.  
  40. /*  Function Prototypes  */
  41.  
  42. GLvoid  initgfx( GLvoid );
  43. GLvoid  drawScene( GLvoid );
  44. GLvoid  reshape( GLsizei, GLsizei );
  45. GLvoid  keyboard( GLubyte, GLint, GLint );
  46.  
  47. void printHelp( char * );
  48.  
  49. static void printColorStrings( GLvoid );
  50. static void renderBitmapString( void *, char * );
  51.  
  52. /* Global Definitions */
  53.  
  54. #define KEY_ESC        27    /* ascii value for the escape key */
  55.  
  56. /* Global Variables */
  57.  
  58. static int deltaY;
  59. static GLint windW = 800, windH = 840;
  60. static GLvoid *font;
  61.  
  62. void
  63. main( int argc, char *argv[] )
  64. {
  65.     GLsizei width, height;
  66.  
  67.     glutInit( &argc, argv );
  68.  
  69.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  70.     height = glutGet( GLUT_SCREEN_HEIGHT );
  71.     glutInitWindowPosition( width/4, height/8); 
  72.     glutInitWindowSize( windW, windH );
  73.     glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
  74.  
  75.     glutCreateWindow( argv[0] );
  76.  
  77.     initgfx();
  78.  
  79.     glutKeyboardFunc( keyboard );
  80.     glutReshapeFunc( reshape );
  81.     glutDisplayFunc( drawScene ); 
  82.  
  83.     printHelp( argv[0] );
  84.  
  85.     glutMainLoop();
  86. }
  87.  
  88. void
  89. printHelp( char *progname )
  90. {
  91.     fprintf(stdout, "\n%s - demonstrates the use of the blend_minmax,\n"
  92.         "blend_subtract, and blend_logic_op extensions\n\n"
  93.         "Escape key            - exit the program\n\n",
  94.         progname);
  95. }
  96.  
  97. void 
  98. initgfx( void )
  99. {
  100.     const GLubyte *s;
  101.     char *extName1 = "GL_EXT_blend_subtract";
  102.     char *extName2 = "GL_EXT_blend_minmax";
  103.     char *extName3 = "GL_EXT_blend_logic_op";
  104.  
  105.     font = GLUT_BITMAP_TIMES_ROMAN_24;
  106.  
  107.     glShadeModel(GL_FLAT);
  108.     glClearColor(0.5, 0.6, 0.1, 1.0);
  109.  
  110.     /* Make sure new blend_* extensions exist */
  111.     if (glutExtensionSupported(extName1) == 0) {
  112.         printf("%s is not supported by the server.\n", extName1);
  113.     }
  114.     if (glutExtensionSupported(extName2) == 0) {
  115.         printf("%s is not supported by the server.\n", extName2);
  116.     }
  117.     if (glutExtensionSupported(extName3) == 0) {
  118.         printf("%s is not supported by the server.\n", extName3);
  119.     }
  120. }
  121.  
  122.  
  123. GLvoid 
  124. keyboard( GLubyte key, GLint x, GLint y )
  125. {
  126.     switch (key) {
  127.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  128.         exit(0);
  129.     }
  130. }
  131.  
  132. GLvoid 
  133. reshape(int width, int height)
  134. {
  135.     windW = (GLint)width;
  136.     windH = (GLint)height;
  137.  
  138.     glViewport(0, 0, (GLint)width, (GLint)height);
  139.     deltaY = windH/14;
  140.  
  141.     glMatrixMode(GL_PROJECTION);
  142.     glLoadIdentity();
  143.     gluOrtho2D(0, windW, 0, windH);
  144.     glMatrixMode(GL_MODELVIEW);
  145. }
  146.  
  147. static void
  148. renderBitmapString( void *font, char *string )
  149. {
  150.     int i;
  151.     int len = (int) strlen(string);
  152.     for (i = 0; i < len; i++) {
  153.             glutBitmapCharacter(font, string[i]);
  154.     }
  155. }
  156.                         
  157. static void 
  158. printColorStrings( GLvoid )
  159. {
  160.     GLubyte ubbuf[3];
  161.     int i, xleft, xright;
  162.     char colorString[20];
  163.  
  164.     xleft = 5 + windW/4;
  165.     xright = 5 + windW/2;
  166.  
  167.     glColor3f(0.8, 0.8, 0.8);
  168.     for (i = windH - deltaY + 4; i > 0; i-=deltaY) {
  169.         glReadPixels(xleft, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, 
  170.                 ubbuf);
  171.         sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  172.             ubbuf[0], ubbuf[1], ubbuf[2]);
  173.         glRasterPos2f(xleft, i);
  174.         renderBitmapString(font,colorString);
  175.  
  176.         glReadPixels(xright, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, 
  177.                 ubbuf);
  178.         sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  179.             ubbuf[0], ubbuf[1], ubbuf[2]);
  180.         glRasterPos2f(xright, i);
  181.         renderBitmapString(font,colorString);
  182.     }
  183. }
  184.  
  185. void 
  186. drawScene(void)
  187. {
  188.     GLubyte ubbuf[3];
  189.     int stringOffset = 5, stringx = 8;
  190.     int i, x1, x2;
  191.  
  192.     glClear(GL_COLOR_BUFFER_BIT);
  193.  
  194.     /* Draw background */
  195.     glColor3f(0.1, 0.1, 1.0);
  196.     glRectf(0.0, 0.0, windW/2, windH);
  197.  
  198.     /* Draw labels */
  199.     glColor3f(0.8, 0.8, 0.0);
  200.     i = windH - deltaY + stringOffset;
  201.     glRasterPos2f(stringx, i);
  202.     renderBitmapString(font,"DEST");
  203.  
  204.     i -= deltaY;
  205.     glRasterPos2f(stringx, i);
  206.     renderBitmapString(font,"SOURCE");
  207.  
  208. #ifdef GL_EXT_blend_subtract
  209.     i -= deltaY;
  210.     glRasterPos2f(stringx, i);
  211.     renderBitmapString(font,"subtract");
  212.  
  213.     i -= deltaY;
  214.     glRasterPos2f(stringx, i);
  215.     renderBitmapString(font,"reverse_subtract");
  216. #endif
  217.  
  218. #ifdef GL_EXT_blend_minmax
  219.     i -= deltaY;
  220.     glRasterPos2f(stringx, i);
  221.     renderBitmapString(font,"min");
  222.  
  223.     i -= deltaY;
  224.     glRasterPos2f(stringx, i);
  225.     renderBitmapString(font,"max");
  226. #endif
  227.  
  228. #ifdef GL_EXT_blend_logic_op
  229.     i -= deltaY;
  230.     glRasterPos2f(stringx, i);
  231.     renderBitmapString(font,"clear");
  232.  
  233.     i -= deltaY;
  234.     glRasterPos2f(stringx, i);
  235.     renderBitmapString(font,"set");
  236.  
  237.     i -= deltaY;
  238.     glRasterPos2f(stringx, i);
  239.     renderBitmapString(font,"copy");
  240.  
  241.     i -= deltaY;
  242.     glRasterPos2f(stringx, i);
  243.     renderBitmapString(font,"noop");
  244.  
  245.     i -= deltaY;
  246.     glRasterPos2f(stringx, i);
  247.     renderBitmapString(font,"and");
  248.  
  249.     i -= deltaY;
  250.     glRasterPos2f(stringx, i);
  251.     renderBitmapString(font,"invert");
  252.  
  253.     i -= deltaY;
  254.     glRasterPos2f(stringx, i);
  255.     renderBitmapString(font,"or");
  256.  
  257.     i -= deltaY;
  258.     glRasterPos2f(stringx, i);
  259.     renderBitmapString(font,"xor");
  260. #endif
  261.  
  262.     /* Leave one rectangle of background color */
  263.     i = windH - deltaY;
  264.     x1 = windW/4;
  265.     x2 = 3 * windW/4;
  266.  
  267.     /* Draw foreground color for comparison */
  268.     i -= deltaY;
  269.     glColor3f(0.9, 0.2, 0.8);
  270.     glRectf(x1, i, x2, i+deltaY);
  271.  
  272.     /* Begin test cases */
  273.     glEnable(GL_BLEND);
  274.  
  275. #ifdef GL_EXT_blend_subtract
  276.     /* set blending factors */
  277.     glBlendFunc(GL_ONE, GL_ONE);
  278.  
  279.     i -= deltaY;
  280.     glBlendEquationEXT(GL_FUNC_SUBTRACT_EXT);
  281.     glRectf(x1, i, x2, i+deltaY);
  282.  
  283.     i -= deltaY;
  284.     glBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
  285.     glRectf(x1, i, x2, i+deltaY);
  286.  
  287.     /* return to default blending factors */
  288.     glBlendFunc(GL_ONE, GL_ZERO);
  289. #endif
  290.  
  291. #ifdef GL_EXT_blend_minmax
  292.     i -= deltaY;
  293.     glBlendEquationEXT(GL_MIN_EXT);
  294.     glRectf(x1, i, x2, i+deltaY);
  295.  
  296.     i -= deltaY;
  297.     glBlendEquationEXT(GL_MAX_EXT);
  298.     glRectf(x1, i, x2, i+deltaY);
  299. #endif
  300.  
  301. #ifdef GL_EXT_blend_logic_op
  302.     glBlendEquationEXT(GL_LOGIC_OP);
  303.  
  304.     i -= deltaY;
  305.     glLogicOp(GL_CLEAR);
  306.     glRectf(x1, i, x2, i+deltaY);
  307.  
  308.     i -= deltaY;
  309.     glLogicOp(GL_SET);
  310.     glRectf(x1, i, x2, i+deltaY);
  311.  
  312.     i -= deltaY;
  313.     glLogicOp(GL_COPY);
  314.     glRectf(x1, i, x2, i+deltaY);
  315.  
  316.     i -= deltaY;
  317.     glLogicOp(GL_NOOP);
  318.     glRectf(x1, i, x2, i+deltaY);
  319.  
  320.     i -= deltaY;
  321.     glLogicOp(GL_AND);
  322.     glRectf(x1, i, x2, i+deltaY);
  323.  
  324.     i -= deltaY;
  325.     glLogicOp(GL_INVERT);
  326.     glRectf(x1, i, x2, i+deltaY);
  327.  
  328.     i -= deltaY;
  329.     glLogicOp(GL_OR);
  330.     glRectf(x1, i, x2, i+deltaY);
  331.  
  332.     i -= deltaY;
  333.     glLogicOp(GL_XOR);
  334.     glRectf(x1, i, x2, i+deltaY);
  335.     glRectf(x1, i+deltaY/2, x2, i+deltaY/2+5);
  336. #endif
  337.     glDisable(GL_BLEND);
  338.  
  339.     printColorStrings();
  340.     glFlush();
  341. }
  342.  
  343.